﻿<?php
function checkMachines($dbh){
	$listing = $dbh->getAllMachine();
		
	foreach($listing as $machine){
		$host= $machine['IP'];
		$port = 30; 
		$waitTimeoutInSeconds = 1; 
		
		if(!fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){
			$types = $dbh->getAllType_Incident();
			$exists = false;
			foreach($types as $val){
				if($val['nom_type'] == 'Ping Problem'){
					$exists = true;
					$id_type = $val['id_type_incident'];
				}
			}
			
			if($exists == true){
				$type_inc = new Type_Incident($dbh, $id_type);
			}else{
				$type_inc = new Type_Incident($dbh);
				$type_inc->setName('Ping Problem');
				$type_inc->save();
			}
			
			$incidents = $dbh->getAllIncidentByMachine($machine['id_machine']);
			
			$exists = false;
			foreach($incidents as $incident){
				if($incident['complement'] == 'Server cannot ping machine'){
					$exists = true;
					$inc = new Incident($dbh, $incident['id_incident']);
					$inc->setType($type_inc->getId());
					$inc->save();
				}
			}
			if($exists == false){
				//on ajoute un nouvel incident
				$incident = new Incident($dbh);
				$incident->setType($type_inc->getId());
				$incident->setIs_resolved('false');
				$incident->setMachine($machine['id_machine']);
				$incident->setComplement('Server cannot ping machine');
				$incident->save();
			}
		}
	}
}